package com.user.payment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.Company;
import com.liferay.portal.service.CompanyLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.Encryptor;
import com.liferay.util.EncryptorException;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.payment.validation.CardValidation;
import com.slayer.model.CreditCard;
import com.slayer.model.impl.CreditCardImpl;
import com.slayer.service.CreditCardLocalServiceUtil;

public class MyCreditCard extends MVCPortlet {

	public void saveAccountDetails(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException, SystemException {
		
		long cardId=0l; String encryptedCardNumber = "";
		ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
		ArrayList<Long> myCardIdList=new ArrayList<Long>();
		List<CreditCard> creditCards=CreditCardLocalServiceUtil.getByUserId_CompanyId(themeDisplay.getUserId(), themeDisplay.getCompanyId());

		int[] ccListIndexes = StringUtil.split(
				ParamUtil.getString(actionRequest, "ccListIndexes"), 0);
		
		for (int i = 0; i < ccListIndexes.length; i++) {

			int accListIndex = ccListIndexes[i];
			cardId = ParamUtil.getLong(actionRequest, "cardId"
					+ accListIndex);
			String cardType = ParamUtil.getString(actionRequest, "cardType"
					+ accListIndex);
			String cardFirstName = ParamUtil.getString(actionRequest,
					"cardFirstName" + accListIndex);
			String cardLastName = ParamUtil.getString(actionRequest,
					"cardLastName" + accListIndex);
			String cardNumber = ParamUtil.getString(actionRequest, "cardNumber"
					+ accListIndex);
			String CVV = ParamUtil.getString(actionRequest, "CVV"
					+ accListIndex);
			String expMM = ParamUtil.getString(actionRequest, "expMM"
					+ accListIndex);
			String expYY = ParamUtil.getString(actionRequest, "expYY"
					+ accListIndex);
			int defaultEntry = ParamUtil.getInteger(actionRequest,
					"defaultEntry");
			try {
				Company company = CompanyLocalServiceUtil.getCompanyById(themeDisplay.getCompanyId());
				encryptedCardNumber = Encryptor.encrypt(company.getKeyObj(), cardNumber);
			} catch (EncryptorException e) {
				e.printStackTrace();
			} catch (PortalException e) {
				e.printStackTrace();
			}
			
			/*
			if(!CardValidation.isValidEntry(actionRequest, cardType, cardFirstName, cardLastName, cardNumber, CVV, expMM, expYY)){
			break;
			}
			*/
			
			cardId=updateCardItem(i,actionRequest,myCardIdList,creditCards,themeDisplay,cardId,cardType,cardFirstName,cardLastName,
					encryptedCardNumber,CVV,expMM,expYY,defaultEntry);
			myCardIdList.add(cardId);
			
			if(ccListIndexes.length == myCardIdList.size()){
				removeCard(accListIndex, actionRequest, myCardIdList, creditCards);
				}
			}
		}

	private long updateCardItem(int i,ActionRequest actionRequest,ArrayList<Long> myCardIdList,List<CreditCard> creditCards,ThemeDisplay themeDisplay,
			long cardId,String cardType,String cardFirstName,String cardLastName,String cardNumber,String CVV,String expMM,String expYY,int defaultEntry){
			
			CreditCard creditCard=null;
		
		if (creditCards == null || creditCards.isEmpty()) {
			List<CreditCard> newList = new ArrayList<CreditCard>();
			newList.add(new CreditCardImpl());
			creditCards = newList;	
		}
		
		if (cardId == 0) {
			try {
				cardId=CounterLocalServiceUtil.increment();
			} catch (SystemException e) {
				e.printStackTrace();
			}
			creditCard=CreditCardLocalServiceUtil.createCreditCard(cardId);
			creditCard.setDateAdded(new java.util.Date());
		}else{
			try {
				creditCard=CreditCardLocalServiceUtil.getCreditCard(cardId);
				creditCard.setDateModified(new Date());
			} catch (PortalException e) {
				e.printStackTrace();
			} catch (SystemException e) {
				e.printStackTrace();
			}
		}
				creditCard.setUserId(themeDisplay.getUser().getUserId());
				creditCard.setCardId(cardId);
				//creditCard.setCardType(cardType);
				creditCard.setCardFirstName(cardFirstName);
				creditCard.setCardLastName(cardLastName);
				creditCard.setCardNumber(cardNumber);
				creditCard.setCardCVV(CVV);
				creditCard.setCardExpMonth(expMM);
				creditCard.setCardExpYear(expYY);
				creditCard.setDateModified(new java.util.Date());
				creditCard.setDefaultEntry(i == defaultEntry);
			try {
				CreditCardLocalServiceUtil.updateCreditCard(creditCard);
				} catch (SystemException e) {
				}
		return cardId;
	}
	private void removeCard(int i,ActionRequest actionRequest, ArrayList<Long> myCardIdList, List<CreditCard> creditCards) throws SystemException{
			for(CreditCard creditCard :creditCards){
				if(creditCards.size()!=1 && !myCardIdList.contains(creditCard.getCardId())){
					CreditCardLocalServiceUtil.deleteCreditCard(creditCard);
				}
			}
		}
	}
